package com.zyy.smartcity1129.movie;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.FeedbackInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.PpUtil;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import okhttp3.RequestBody;

/**
 * @ClassName: MovieFeedback$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 23/11/2021$
 * @Version: 1.0
 */
public class MovieFeedbackFragment extends BaseFragment {
    List<FeedbackInfo.RowsBean> feedbackList = new ArrayList<>();
    ExecutorService executor = Executors.newFixedThreadPool(1);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    private RecyclerView rvMovieFeedback;
    private TextView tvMovieFeedback;
    RvAd<FeedbackInfo.RowsBean> rvAd;

    @Override
    protected int layId() {
        return R.layout.fragment_movie_feedback;
    }

    @Override
    protected String title() {
        return "意见反馈";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                feedbackList = Http.getInstance().get("/prod-api/api/movie/feedback/list", null, FeedbackInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (mInteger.get() == 1) {
                initAdapter(feedbackList);
                rvMovieFeedback.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMovieFeedback.setAdapter(rvAd);
            }
        });
        tvMovieFeedback.setOnClickListener(v1 -> {
            PpUtil p = new PpUtil();
            View view = p.getP(requireContext(), R.layout.p_feedback_add);
            FrameLayout pF1 = (FrameLayout) view.findViewById(R.id.p_f1);
            EditText editPContext = (EditText) view.findViewById(R.id.editPContext);
            EditText editPTitle = (EditText) view.findViewById(R.id.editPTitle);
            Button btnPSubmit = (Button) view.findViewById(R.id.btnPSubmit);
            FrameLayout pF2 = (FrameLayout) view.findViewById(R.id.p_f2);
            pF1.setOnClickListener(v2 -> {
                p.closeP();
            });
            pF2.setOnClickListener(v2 -> {
                p.closeP();
            });
            btnPSubmit.setOnClickListener(v2 -> {
                String title = editPTitle.getText().toString().trim();
                String context = editPContext.getText().toString().trim();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty("title", title);
                        jsonObject.addProperty("content", context);
                        String result = Http.getInstance().get("/prod-api/api/movie/feedback",
                                RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                        feedbackList = Http.getInstance().get("/prod-api/api/movie/feedback/list",
                                null, FeedbackInfo.class, 1).getRows();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                initAdapter(feedbackList);
                                rvMovieFeedback.setAdapter(rvAd);
                            }
                        });
                    }
                }).start();
                p.closeP();
            });
        });
    }

    private void initAdapter(List<FeedbackInfo.RowsBean> feedbackList) {
        rvAd = new RvAd<FeedbackInfo.RowsBean>(feedbackList, R.layout.rv_feedback) {
            private TextView tvRvFeedBack1, tvRvFeedBack2, tvRvFeedBack3;
            private void initView(View v) {
                tvRvFeedBack1 = (TextView) v.findViewById(R.id.tvRvFeedBack1);
                tvRvFeedBack2 = (TextView) v.findViewById(R.id.tvRvFeedBack2);
                tvRvFeedBack3 = (TextView) v.findViewById(R.id.tvRvFeedBack3);
            }
            @Override
            protected void initR(View iv, FeedbackInfo.RowsBean rowsBean, int p) {
                initView(iv);
                tvRvFeedBack1.setText(rowsBean.getTitle());
                tvRvFeedBack2.setText("提交人：" + rowsBean.getUserId());
                tvRvFeedBack3.setText(rowsBean.getContent());
            }
        };
    }

    private void initView(View v) {
        rvMovieFeedback = (RecyclerView) v.findViewById(R.id.rvMovieFeedback);
        tvMovieFeedback = (TextView) v.findViewById(R.id.tvMovieFeedback);
    }
}
